import {isArray, isObject} from "./is";

export const guid = () => {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
        return v.toString(16);
    });
}

export const clone = o => {
    var res;
    if(isArray(o)){
        res = [];
    }else if(isObject(o)){
        res = {};
    }else{
        return o;
    }

    if(isArray(o)) {
        for (let i = 0, ni = o.length; i < ni; i++) {
            res.push(clone(o[i]));
        }
        return res;
    }else if( isObject(o)) {
        for(let i in o) {
            res[i] = clone(o[i]);
        }
        return res;
    }


}


/*
const _path = '_path';
const _val = '_val';
const _genPath = (path, subpath) => {
    let res = [];
    if(path !== ''){
        res.push(path);
    }
    if(subpath !== ''){
        res.push(subpath);
    }
    return res.join('.');
}

const loopDep = (o, path) => {
    if(isArray(o)){
        o.forEach((e, index) => {
            if(isObject(e)){
                let p = _genPath(path, index);
                e[_path] = p;
                loopDep(e, p)
            }else if(isString(e) || isNumber(e) || isBoolean(e)){
                o[index] = {};
                o[index][_path] = _genPath(path, index);
                o[index][_val] = e;
            }
        })
    }else if(isObject(o)){
        const lists = Object.keys(o);
        lists.forEach(key => {
            if(key !== _path && key !== _val){
                const val = o[key];
                let p = _genPath(path, key);
                if(isString(val) || isNumber(val) || isBoolean(val)){
                    o[key] = {};
                    o[key][_path] = p;
                    o[key][_val] = val;
                }else if(isArray(val)){
                    o[key] = {};
                    o[key][_path] = p;
                    o[key][_val] = val;
                    loopDep(o[key][_val], p);
                }else{
                    o[key][_path] = p;
                    loopDep(o[key], p);
                }
            }
        })
    }
} 
 */